package LBLD新算法;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static MyUtils.MHUtils.*;
import static MyUtils.MHMoban.*;

/*
    每个节点只处理自己（单cur思想）
    做完题一定写入表格
*/
public class 滑动窗口最大值239 {
    @Test
    public void test() {
        int[] p = {1,3,-1,-3,5,3,6,7};
//        int[] ints = maxSlidingWindow(p, 3);
        int[] ints = maxSlidingWindow(new int[]{1}, 1);
        System.out.println(Arrays.toString(ints));
    }
    //ac-超时
    /*
        最后r++l++(别上来++)

    */
    public int[] maxSlidingWindow(int[] nums, int k) {
        int l=0,r=0;
        Map<Integer,Integer> map = new HashMap<>();
        int max = nums[r];
        int[] res=new int[nums.length-k+1];
        while (r<nums.length){
            map.put(nums[r], map.getOrDefault(nums[r],0)+1);
            if(nums[r]>max) max=nums[r];
            r++;
            if(r-l==k){
                res[l]=max;
                int lVal = nums[l];
                map.put(lVal, map.get(lVal)-1);
                //必须是max出现个数=0，才更新m
                if(lVal==max && map.get(lVal)==0){
                    int m=-99999;
                    for (Integer num : map.keySet()) {
                        //光有key不行，还要个数>=1
                        if(map.get(num)>0) {
                            if (num > m) m = num;
                        }
                    }
                    max=m;
                }
                l++;
            }
        }
        return res;
    }
}
